package butines.common.game;

import java.awt.Graphics2D;

import butines.core.GameCore;

public abstract class StateBasedGame extends GameCore {

	private GameState currentState;
	private GameState nextState;
	
	public StateBasedGame() {

	}
	
	private void changeState() { 
		if (nextState != null) {
			if (currentState != null) {
				currentState.leave();
			}
			currentState = nextState;
			currentState.enter();
			nextState = null;
		}
	}
	
	@Override
	public void update(float tpf) {
		
		changeState();
		
		preUpdateState(tpf);
		if (currentState != null) {
			currentState.update(tpf);
		}
		posUpdateState(tpf);
	}

	@Override
	public void render(Graphics2D g2d) {
		preRenderState(g2d);
		if (currentState != null) {
			currentState.render(g2d);
		}
		posRenderState(g2d);
	}

	public void enterState(GameState state) {
		if (state == null) {
			throw new IllegalArgumentException();
		}
		nextState = state;
	}
	
	protected void preUpdateState(float tpf) {
		
	}
	
	protected void posUpdateState(float tpf) {
		
	}
	
	protected void preRenderState(Graphics2D g2d) {
		
	}
	
	protected void posRenderState(Graphics2D g2d) {
		
	}
	
}
